package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TUnion;
import com.pageModel.DataGrid;
import com.pageModel.Union;
import com.service.school.UnionServiceI;

@Service(value = "unionService")
public class UnionServiceImpl implements UnionServiceI{

	private BaseDaoI<TUnion> unionDao;

	public BaseDaoI<TUnion> getUnionDao() {
		return unionDao;
	}
	@Autowired
	public void setUnionDao(BaseDaoI<TUnion> unionDao) {
		this.unionDao = unionDao;
	}
	@Override
	public void add(Union union) {
		TUnion t=new TUnion();
		t.setUnionId(UUID.randomUUID().toString());
		t.setUnionName(union.getUnionName());
		t.setUnionText(union.getUnionText());
		t.setUnionAddress(union.getUnionAddress());
		t.setUnionEmail(union.getUnionEmail());
		t.setUnionMan(union.getUnionMan());
		t.setUnionPlace(union.getUnionPlace());
		t.setUnionQq(union.getUnionQq());
		t.setUnionTel(union.getUnionTel());
		t.setUnionType("审核中");
		unionDao.save(t);
	}
	@Override
	public DataGrid datagrid(Union union,String shType) {
		DataGrid dg=new DataGrid();
		String hql= null;
		if (shType == null) {
			hql="from TUnion t where 1=1 ";
		} else {
			hql="from TUnion t where t.unionType ='"+shType+"' ";
		}
		if(union.getUnionId()!=null&&!union.getUnionId().trim().equals("")){
			hql+=" and t.unionId like '%"+union.getUnionId()+"%'";
		}
		if(union.getUnionName()!=null&&!union.getUnionName().trim().equals("")){
			hql+=" and t.unionName like'%"+union.getUnionName()+"%'";
		}
		if(union.getUnionPlace()!=null&&!union.getUnionPlace().trim().equals("")){
			hql+=" and t.unionPlace like'%"+union.getUnionPlace()+"%'";
		}
		if(union.getUnionMan()!=null&&!union.getUnionMan().trim().equals("")){
			hql+=" and t.unionMan like'%"+union.getUnionMan()+"%'";
		}
//		if(union.getUnionType()!=null&&!union.getUnionType().trim().equals("")){
//			hql+=" and t.unionType='"+shType+"'";
//		}
		String totalHql = "select count(*) " + hql;
		List<TUnion> l=unionDao.find(hql,union.getPage(),union.getRows());
		List<Union> nl=new ArrayList<Union>();
		if(l!=null&&l.size()>0){
			for(TUnion t:l){
				Union u=new Union();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(unionDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public DataGrid datagrid_SH(Union union){
		if(union.getUnionType()==null||union.getUnionType().trim().equals("")){
			return datagrid(union,"审核中");
		}else if(union.getUnionType().equals("所有")){
			return datagrid(union, null);
		}else{
			return datagrid(union,union.getUnionType());
		}
	}
	@Override
	public DataGrid datagrid_TG(Union union) {
		return datagrid(union,union.getUnionType());
	}
	@Override
	public DataGrid datagrid_BTG(Union union) {
		return datagrid(union,"不通过");
	}
	@Override
	public void edit_TG(Union union) {
		TUnion t=unionDao.get(TUnion.class, union.getUnionId());
		t.setUnionType("通过");
		unionDao.update(t);
	}  
	@Override
	public void edit_BTG(Union union) {
		TUnion t=unionDao.get(TUnion.class, union.getUnionId());
		t.setUnionType("不通过");
		unionDao.update(t);
	}
	@Override
	public List<TUnion> getUnionCmb(Union union) {
		String hql="from TUnion t ";
		List<TUnion> l = unionDao.find(hql);
		return l;
	}
	@Override
	public DataGrid datagrid(Union union) {
		DataGrid dg=new DataGrid();
		String hql="from TUnion t where unionType in('通过','停用') ";
		if(null!=union.getUnionName()&&!union.getUnionName().trim().equals("")){
			hql+="and t.unionName like '%"+union.getUnionName()+"%'";
		}
		String totalHql = "select count(*) " + hql;
		List<TUnion> l=unionDao.find(hql,union.getPage(),union.getRows());
		List<Union> nl=new ArrayList<Union>();
		if(l!=null&&l.size()>0){
			for(TUnion t:l){
				Union u=new Union();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(unionDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public void edit_TY(Union union) {
		TUnion t=unionDao.get(TUnion.class, union.getUnionId());
		t.setUnionType("停用");
		unionDao.update(t);
	}  
	
}
